package com.paziy.jan.matrix;

import com.paziy.jan.fraction.*;

import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: paziy
 * Date: 08.10.12
 * Time: 17:13
 * To change this template use File | Settings | File Templates.
 */

public class Matrix {
    //TODO make interface for items of matrix
    public Fraction[][] items;
    public int rowsCount, columnsCount;

    private int row, column;

    public Matrix(int rowsCount, int columnsCount) {
        this.rowsCount = rowsCount;
        this.columnsCount = columnsCount;
        items = new Fraction[rowsCount][columnsCount];
    }

    public Matrix(double[][] items) {
        this.rowsCount = items.length;
        this.columnsCount = items[0].length;
        //this.items = items;
    }

    public Matrix minor(int rowNumber, int columnNumber) {
        Matrix minor = new Matrix(rowsCount - 1, columnsCount - 1);
        int newRow, newColumn;

        for (row = 0; row < rowsCount; row++) {
            for (column = 0; column < columnsCount; column++) {
                if (row != rowNumber && column != columnNumber) {
                    newRow = row;
                    if (rowNumber < row) newRow--;

                    newColumn = column;
                    if (columnNumber < column) newColumn--;

                    minor.items[newRow][newColumn] = this.items[row][column];
                }
            }
        }

        return minor;
    }

    public static Matrix random(int rowsCount, int columnsCount) {
        Matrix result = new Matrix(rowsCount, columnsCount);
        Random rand = new Random();
        int numerator, denominator;

        for (int row = 0; row < rowsCount; row++) {
            for (int column = 0; column < columnsCount; column++) {

                numerator = rand.nextInt(300) - rand.nextInt(150);
                do {
                    denominator = rand.nextInt(300) - rand.nextInt(150);
                } while (denominator == 0);

                result.items[row][column] = new Fraction(numerator,denominator);
            }
        }

        return result;
    }

    //TODO make clone method

    //TODO make output stream class
    void print() {
        for (row = 0; row < rowsCount; row++) {
            for (column = 0; column < columnsCount; column++)
                System.out.print(items[row][column] + "  ");
            System.out.print("\n");
        }
    }

}
